package com.example.RemindMe;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CreateAlarm extends Activity {

	TimePicker timePicker;
	Calendar calendar = Calendar.getInstance();
	Date alarmTime = new Date();
	EditText editText;
	SharedPreferences sharedPreferences;

	CalendarView cv;


	EditText description;
	Spinner snooze;
	String uriSelected;
	SharedPreferences sharedPreferencesSettings;
	SharedPreferences sharedPreferencesLastId;
	Button btnRingtone;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setRequestedOrientation(1);
		setContentView(R.layout.activity_setalarm);
        findViewById(R.id.linearLayout).requestFocus();
		timePicker = (TimePicker) findViewById(R.id.timePicker1);

		cv = (CalendarView) findViewById(R.id.calendarView1);


		timePicker.setIs24HourView(true);
		timePicker.setCurrentHour(07);
		timePicker.setCurrentMinute(00);

		description = (EditText) findViewById(R.id.editText1);
		snooze = (Spinner) findViewById(R.id.spinner2);

		sharedPreferences = getSharedPreferences("alarms", 0);
		sharedPreferencesLastId = getSharedPreferences("lastid", 0);
		sharedPreferencesSettings = getSharedPreferences("settingstab", 0);
		uriSelected = sharedPreferencesSettings.getString("urialarm",
				"content://media/internal/audio/media/23");
		String ringtoneTitle = sharedPreferencesSettings.getString(
				"titlealarm", "no sound in the settings");
		btnRingtone = (Button) findViewById(R.id.button2);
		btnRingtone.setText(ringtoneTitle);
		String snoozeDauer = sharedPreferencesSettings.getString("snooze", "0");
		snooze.setSelection(Integer.parseInt(snoozeDauer));
		/* First Tab Content */

	}

	/**
	 * 
	 * Methode zum Anlegen eines neuen Alarms. Alarm wird mittels AlarmManager
	 * angelegt und die zugeh�rigen Daten in einer SharedPreference gespeichert,
	 * um den Alarm nachtr�glich zu ver�ndern/zu l�schen.
	 * 
	 */
	@SuppressWarnings("deprecation")
	public void setNewAlarm(View v) {
		int hour = timePicker.getCurrentHour();
		int minute = timePicker.getCurrentMinute();


		calendar.setTimeInMillis(cv.getDate());
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);


		Intent intent = new Intent(this, AlarmReceiver.class);

		Editor editorId = sharedPreferencesLastId.edit();
		if (sharedPreferencesLastId.getInt("lastid", 0) <= 1) {

			editorId.putInt("lastid", 1);
			editorId.commit();
		}

		int random = sharedPreferencesLastId.getInt("lastid", 1) + 1;

		editorId.putInt("lastid", random);
		editorId.commit();

		intent.putExtra("id", random);
		PendingIntent sender = PendingIntent.getBroadcast(this, random, intent,
                0);

		AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
		am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
		Toast.makeText(this, "alarm is set" + calendar.getTime(),
                Toast.LENGTH_LONG).show();

		Editor editor = sharedPreferences.edit();

		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
		String newDate = sdf.format(calendar.getTime());

		editor.putString("" + random, newDate + ";" + description.getText()
				+ ";" + uriSelected + ";" + snooze.getSelectedItemPosition());

		editor.commit();
		finish();
		Intent alarm = new Intent(getBaseContext(), AlarmTab.class);
		startActivity(alarm);

        //geändert
        finish();

	}

	/**
	 * 
	 * RingtonePicker
	 * 
	 */
	public void setRingtone(View v) {

		Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
		intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
				"Select ringtone for notifications:");
		intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
		intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
		intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
				RingtoneManager.TYPE_NOTIFICATION);

		this.startActivityForResult(intent, 999);

	}

	/**
	 * 
	 * Diese Methode wird aufgerufen, nachdem ein Alarmton ausgew�hlt wird. Die
	 * URI des ausgew�hlten Alarms wird unter anderem auch in den
	 * SharedPreferences gespeichert.
	 * 
	 */
	protected void onActivityResult(int requestCode, int resultCode,
			Intent Mringtone) {
		switch (resultCode) {

		case RESULT_OK:
			Uri uri = Mringtone
					.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

			Ringtone titel = RingtoneManager.getRingtone(this, uri);

			uriSelected = uri.toString();
			btnRingtone.setText(titel.getTitle(this));
			Log.i("Sample", "uri " + uri);

			break;
		}

	}

}
